package cn.csc.framework.pay.core.client.impl.tl.util;

import cn.csc.framework.pay.core.client.impl.tl.TLPayClientConfig;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;

@Slf4j
public class SybPayService {

    private TLPayClientConfig config;

    public SybPayService(TLPayClientConfig config) {
        this.config = config;
    }

    /**
     * 统一支付接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15#mid=88
     * @param trxamt 交易金额,单位为分
     * @param reqsn 商户交易单号
     * @param paytype 交易方式
     * @param body 订单标题
     * @param remark 备注
     * @param acct 支付平台用户标识,S支付时使用 微信支付-用户的微信openid,支付宝支付-用户user_id,微信小程序-用户小程序的openid,云闪付JS-用户userId
     * @param validtime 有效时间,订单有效时间，以分为单位，不填默认为5分钟
     * @param notify_url 交易结果通知地址
     * @param limit_pay 支付限制
     * @param idno 证件号
     * @param truename 付款人真实姓名
     * @param asinfo 分账信息
     * @param sub_appid 微信子appid,微信小程序/微信公众号/APP的appid
     * @param goods_tag   单品优惠信息
     * @param chnlstoreid 渠道门店编号
     * @param subbranch 门店号
     * @param
     * @param cusip       限云闪付JS支付业务
     * @param fqnum       限支付宝分期业务
     * @return
     * @throws Exception
     */
    public Map<String, String> pay(long trxamt, String reqsn, String paytype,
                                   String body, String remark, String acct,
                                   String validtime, String notify_url, String limit_pay,
                                   String idno, String truename, String asinfo,
                                   String sub_appid, String goods_tag, String benefitdetail,
                                   String chnlstoreid, String subbranch, String extendparams,
                                   String cusip, String fqnum, String orgid, String cusid,
                                   String appId, String singType, String priveteKey) throws Exception {
        HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_APIURL + "/pay");
        http.init();
        TreeMap<String, String> params = new TreeMap<String, String>();
        if (!SybUtil.isEmpty(orgid))
            params.put("orgid", orgid);
        params.put("cusid", cusid);
        params.put("appid", appId);
        params.put("version", "11");
        params.put("trxamt", String.valueOf(trxamt));
        params.put("reqsn", reqsn);
        params.put("paytype", paytype);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("body", body);
        params.put("remark", remark);
        params.put("validtime", validtime);
        params.put("acct", acct);
        params.put("notify_url", notify_url);
        params.put("limit_pay", limit_pay);
        params.put("sub_appid", sub_appid);
        params.put("goods_tag", goods_tag);
        params.put("benefitdetail", benefitdetail);
        params.put("chnlstoreid", chnlstoreid);
        params.put("subbranch", subbranch);
        params.put("extendparams", extendparams);
        params.put("cusip", cusip);
        params.put("fqnum", fqnum);
        params.put("idno", idno);
        params.put("truename", truename);
        params.put("asinfo", asinfo);
        params.put("signtype", singType);
        params.put("sign", SybUtil.unionSign(params, priveteKey, singType));
        byte[] bys = http.postParams(params, true);
        String result = new String(bys, "UTF-8");
        Map<String, String> map = handleResult(result);
        return map;

    }

    /**
     * 网上收银统一下单 : 统一支付接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15#mid=88
     */
    public Map<String, String> pay(long trxAmt, String reQsn, String payType,
                                   String body, String remark, String acct,
                                   String validTime, String notifyUrl, String limitPay,
                                   String idNo, String trueName, String asInfo,
                                   String subAppId, String goodsTag, String benefitDetail,
                                   String chnlStoreId, String subbranch, String extendParams,
                                   String cusIp, String fqNum) throws Exception {
        return pay(trxAmt, reQsn, payType, body, remark, acct, validTime, notifyUrl, limitPay, idNo, trueName, asInfo,
                subAppId, goodsTag, benefitDetail, chnlStoreId, subbranch, extendParams, cusIp, fqNum, false);
    }

    /**
     * 网上收银统一下单 : 统一支付接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15#mid=88
     * @param trxAmt 交易金额,单位为分
     * @param reQsn 商户交易单号
     * @param payType 交易方式。 参见：https://aipboss.allinpay.com/know/devhelp/main.php?pid=15#mid=98
     * @param body 订单标题
     * @param remark 备注
     * @param acct 支付平台用户标识,S支付时使用 微信支付-用户的微信openid,支付宝支付-用户user_id,微信小程序-用户小程序的openid,云闪付JS-用户userId
     * @param validTime 有效时间,订单有效时间，以分为单位，不填默认为5分钟
     * @param notifyUrl 交易结果通知地址
     * @param limitPay 支付限制
     * @param idNo 证件号
     * @param trueName 付款人真实姓名
     * @param asInfo 分账信息
     * @param subAppId 微信子appid,微信小程序/微信公众号/APP的appid
     * @param goodsTag   单品优惠信息
     * @param chnlStoreId 渠道门店编号
     * @param subbranch 门店号
     * @param extendParams 拓展参数
     * @param cusIp       限云闪付JS支付业务
     * @param fqNum       限支付宝分期业务
     * @return
     * @throws Exception
     */
    public Map<String, String> pay(long trxAmt, String reQsn, String payType,
                                   String body, String remark, String acct,
                                   String validTime, String notifyUrl, String limitPay,
                                   String idNo, String trueName, String asInfo,
                                   String subAppId, String goodsTag, String benefitDetail,
                                   String chnlStoreId, String subbranch, String extendParams,
                                   String cusIp, String fqNum, Boolean testFlag) throws Exception {
        String url ;
        if (testFlag){
            url = config.getServerUrlMap().get("test") + "apiweb/unitorder/pay";
        } else {
            url = config.getServerUrlMap().get("syb") + "apiweb/unitorder/pay";
        }
        HttpConnectionUtil http = new HttpConnectionUtil(url);
        http.init();
        TreeMap<String, String> params = new TreeMap<>();
        if (StringUtils.hasLength(config.getOrgId()))
            params.put("orgid", config.getOrgId());
        params.put("cusid", config.getMchId());
        params.put("appid", config.getAppId());
        params.put("version", "11");
        params.put("trxamt", String.valueOf(trxAmt));
        params.put("reqsn", reQsn);
        params.put("paytype", payType);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("body", body);
        params.put("remark", remark);
        params.put("validtime", validTime);
        params.put("acct", acct);
        params.put("notify_url", notifyUrl);
        params.put("limit_pay", limitPay);
        params.put("sub_appid", subAppId);
        params.put("goods_tag", goodsTag);
        params.put("benefitdetail", benefitDetail);
        params.put("chnlstoreid", chnlStoreId);
        params.put("subbranch", subbranch);
        params.put("extendparams", extendParams);
        params.put("cusip", cusIp);
        params.put("fqnum", fqNum);
        params.put("idno", idNo);
        params.put("truename", trueName);
        params.put("asinfo", asInfo);
        params.put("signtype", config.getSignType());
        params.put("sign", SybUtil.unionSign(params, config.getPrivateKey(), config.getSignType()));
        byte[] bys = http.postParams(params, true);
        String result = new String(bys, "UTF-8");
        log.info("pay result: {}",result);
        Map<String, String> map = handleResult(result);
        return map;

    }


    public Map<String, String> cancel(long trxamt, String reqsn, String oldtrxid, String oldreqsn) throws Exception {
        HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_APIURL + "/cancel");
        http.init();
        TreeMap<String, String> params = new TreeMap<String, String>();
        if (!SybUtil.isEmpty(SybConstants.SYB_ORGID))
            params.put("orgid", SybConstants.SYB_ORGID);
        params.put("cusid", SybConstants.SYB_CUSID);
        params.put("appid", SybConstants.SYB_APPID);
        params.put("version", "11");
        params.put("trxamt", String.valueOf(trxamt));
        params.put("reqsn", reqsn);
        params.put("oldtrxid", oldtrxid);
        params.put("oldreqsn", oldreqsn);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("signtype", SybConstants.SIGN_TYPE);
        String appkey = "";
        if (SybConstants.SIGN_TYPE.equals("RSA"))
            appkey = SybConstants.SYB_RSACUSPRIKEY;
        else if (SybConstants.SIGN_TYPE.equals("SM2"))
            appkey = SybConstants.SYB_SM2PPRIVATEKEY;
        else
            appkey = SybConstants.SYB_MD5_APPKEY;
        params.put("sign", SybUtil.unionSign(params, appkey, SybConstants.SIGN_TYPE));
        byte[] bys = http.postParams(params, true);
        String result = new String(bys, "UTF-8");
        Map<String, String> map = handleResult(result);
        return map;
    }

    /**
     * 网上收银统一下单 : 统一退款接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15&from=groupmessage&isappinstalled=0#mid=838
     * @param trxAmt 退款金额
     * @param reQsn 商户退款订单号
     * @param oldTrxId 原交易订单号
     * @param oldReQsn 原交易流水
     * @param orgId 集团/代理商商户号
     * @param cusId 商户号
     * @param appId 应用ID
     * @param singType 签名方式
     * @param privateKey 商户秘钥
     * @return
     * @throws Exception
     */
    public JSONObject refund(long trxAmt, String reQsn, String oldTrxId, String oldReQsn,
                             String orgId, String cusId, String appId, String singType, String privateKey) throws Exception {
        return refund(trxAmt, reQsn, oldTrxId, oldReQsn, orgId, cusId, appId, singType, privateKey, false);
    }

    /**
     * 网上收银统一下单 : 统一退款接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15&from=groupmessage&isappinstalled=0#mid=838
     * @param trxAmt 退款金额
     * @param reQsn 商户退款订单号
     * @param oldTrxId 原交易订单号
     * @param oldReQsn 原交易流水
     * @param orgId 集团/代理商商户号
     * @param cusId 商户号
     * @param appId 应用ID
     * @param singType 签名方式
     * @param privateKey 商户秘钥
     * @return
     * @throws Exception
     */
    public JSONObject refund(long trxAmt, String reQsn, String oldTrxId, String oldReQsn,
                                      String orgId, String cusId, String appId, String singType, String privateKey, Boolean testFlag) throws Exception {
        String url ;
        if (testFlag){
            url = config.getServerUrlMap().get("test") + "apiweb/tranx/refund";
        } else {
            url = config.getServerUrlMap().get("syb") + "apiweb/tranx/refund";
        }
        HttpConnectionUtil http = new HttpConnectionUtil(url);
        http.init();
        TreeMap<String, String> params = new TreeMap<>();
        if (!SybUtil.isEmpty(orgId))
            params.put("orgid", orgId);
        params.put("cusid", cusId);
        params.put("appid", appId);
        params.put("version", "11");
        params.put("trxamt", String.valueOf(trxAmt));
        params.put("reqsn", reQsn);
        params.put("oldreqsn", oldReQsn);
        params.put("oldtrxid", oldTrxId);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("signtype", singType);
        params.put("sign", SybUtil.unionSign(params, privateKey, singType));
        byte[] bys = http.postParams(params, true);
        return JSONObject.parseObject(new String(bys,"UTF-8"));
    }

    /**
     * 交易查询
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15&from=groupmessage&isappinstalled=0#mid=836
     * @param reQsn 商户订单号
     * @param trxId 平台交易流水
     * @return
     * @throws Exception
     */
    public JSONObject query(String reQsn, String trxId) throws Exception {
        return query(reQsn, trxId, false);
    }


    /**
     * 交易查询
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=15&from=groupmessage&isappinstalled=0#mid=836
     * @param reQsn 商户订单号
     * @param trxId 平台交易流水
     * @return
     * @throws Exception
     */
    public JSONObject query(String reQsn, String trxId, Boolean testFlag) throws Exception {
        String url ;
        if (testFlag){
            url = config.getServerUrlMap().get("test") + "apiweb/tranx/query";
        } else {
            url = config.getServerUrlMap().get("syb") + "apiweb/tranx/query";
        }
        HttpConnectionUtil http = new HttpConnectionUtil(url);
        http.init();
        TreeMap<String, String> params = new TreeMap<String, String>();
        if (StringUtils.hasLength(config.getOrgId()))
            params.put("orgid", config.getOrgId());
        params.put("cusid", config.getMchId());
        params.put("appid", config.getAppId());
        params.put("version", "11");
        params.put("reqsn", reQsn);
        params.put("trxid", trxId);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("signtype", config.getSignType());
        params.put("sign", SybUtil.unionSign(params, config.getPrivateKey(), config.getSignType()));
        byte[] bys = http.postParams(params, true);
        return JSONObject.parseObject(new String(bys,"UTF-8"));
    }


    public Map<String, String> handleResult(String result) throws Exception {
        Map map = SybUtil.json2Obj(result, Map.class);
        if (map == null) {
            map = new HashMap();
            map.put("retcode", "FAIL");
            map.put("errmsg", "返回数据为空,调用异常");
            map.put("trxstatus", "-99999");
            return map;
        }
        if ("FAIL".equals(map.get("retcode"))) {
            map.put("retcode", "FAIL");
            map.put("errmsg", map.get("retmsg"));
            map.put("trxstatus", "-99999");
            return map;
        }
        if ("SUCCESS".equals(map.get("retcode"))) {
            if ("0000".equals(map.get("trxstatus"))) {
                return map;
            }
        }
        return map;
    }

    public Map<String, String> scanPay(long trxamt, String reqsn, String body, String remark, String authcode, String limit_pay, String idno, String truename, String asinfo) throws Exception {
        // TODO Auto-generated method stub
        HttpConnectionUtil http = new HttpConnectionUtil(SybConstants.SYB_APIURL + "/scanqrpay");
        http.init();
        TreeMap<String, String> params = new TreeMap<String, String>();
        if (!SybUtil.isEmpty(SybConstants.SYB_ORGID))
            params.put("orgid", SybConstants.SYB_ORGID);
        params.put("cusid", SybConstants.SYB_CUSID);
        params.put("appid", SybConstants.SYB_APPID);
        params.put("version", "11");
        params.put("trxamt", String.valueOf(trxamt));
        params.put("reqsn", reqsn);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("body", body);
        params.put("remark", remark);
        params.put("authcode", authcode);
        params.put("limit_pay", limit_pay);
        params.put("asinfo", asinfo);
        params.put("signtype", SybConstants.SIGN_TYPE);
        String appkey = "";
        if (SybConstants.SIGN_TYPE.equals("RSA"))
            appkey = SybConstants.SYB_RSACUSPRIKEY;
        else if (SybConstants.SIGN_TYPE.equals("SM2"))
            appkey = SybConstants.SYB_SM2PPRIVATEKEY;
        else
            appkey = SybConstants.SYB_MD5_APPKEY;
        params.put("sign", SybUtil.unionSign(params, appkey, SybConstants.SIGN_TYPE));
        byte[] bys = http.postParams(params, true);
        String result = new String(bys, "UTF-8");
        Map<String, String> map = handleResult(result);
        return map;
    }

    /**
     * 获取对账单接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=19#mid=109
     * @param api  接口URI, 生产:cusapi/trxfile/get, 测试：vsppcusapi/trxfile/get
     * @param date  交易日期 yyyymmdd
     * @param randomstr 随机字符串,商户自行生成的随机字符串
     * @param filetype 对账文件类型,为空时传统对账单 1：传统对账单 2：数币对账单
     * @return map
     * @throws Exception
     */
    public Map<String, String> trxFileGet(String api, String date, String randomstr, String filetype) throws Exception{
        return trxFileGet(api,date,randomstr,filetype,false);
    }
    /**
     * 获取对账单接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=19#mid=109
     * @param api  接口URI, 生产:cusapi/trxfile/get, 测试：vsppcusapi/trxfile/get
     * @param date  交易日期 yyyymmdd
     * @param randomStr 随机字符串,商户自行生成的随机字符串
     * @param filetype 对账文件类型,为空时传统对账单 1：传统对账单 2：数币对账单
     * @return map
     * @throws Exception
     */
    public Map<String, String> trxFileGet(String api, String date, String randomStr, String filetype, Boolean testFlag) throws Exception {
        String url;
        if (testFlag){
            url = config.getServerUrlMap().get("test") + "vsppcusapi/trxfile/get";
        } else {
            url = config.getServerUrlMap().get("cus") + Optional.ofNullable(api).orElse("cusapi/trxfile/get");
        }
        HttpConnectionUtil http = new HttpConnectionUtil(url);
        http.init();
        TreeMap<String, String> params = new TreeMap<String, String>();
        if (!SybUtil.isEmpty(config.getOrgId()))
            params.put("orgid", config.getOrgId());
        params.put("cusid", config.getMchId());
        params.put("appid", config.getAppId());
        params.put("date", date);
        params.put("randomstr", randomStr);
        params.put("filetype", filetype);
        params.put("signtype", config.getSignType());
        params.put("sign", SybUtil.unionSign(params, config.getPrivateKey(), config.getSignType()));

        byte[] bys = http.postParams(params, true);
        String result = new String(bys, "UTF-8");
        Map<String, String> map = handleResult(result);
        return map;
    }

    /**
     * H5收银台订单提交接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=38#mid=313
     * @return
     * @throws Exception
     */
    public JSONObject unionOrder(String api, String version, long trxAmt, String payType, String fqNum, String reQsn,
                                 String charset, String retUrl, String notifyUrl, String body, String remark,
                                 String validTime, String limitPay, String goodsTag, String benefitDetail, String asInfo,
                                 String subbranch, String extendParams, String trueName, String idNo,
                                 String unPid, String terminfo) throws Exception {
        return unionOrder(api, version, trxAmt, payType, fqNum, reQsn, charset, retUrl, notifyUrl, body, remark, validTime, limitPay, goodsTag, benefitDetail, asInfo, subbranch, extendParams, trueName, idNo, unPid, terminfo, false);
    }

    /**
     * H5收银台订单提交接口
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=38#mid=313
     * @param api 接口URI, apiweb/h5unionpay/unionorder
     * @param version 版本号 默认为12
     * @param trxAmt 付款金额 单位为分
     * @param payType 指定交易类型 当指定为F02时，交易仅限分期交易。分期交易金额必须大于500元。 参见：https://aipboss.allinpay.com/know/devhelp/main.php?pid=38#mid=320
     * @param fqNum 支付宝分期 暂只支持支付宝花呗分期,支付宝信用卡分期 仅支持A01/A02
     * @param reQsn 商户唯一订单号
     * @param charset 参数字符编码集。商户网站使用的编码格式，支持 UTF-8、GBK 跟商户网站的编码一致
     * @param retUrl 页面跳转同步通知页面路径
     * @param notifyUrl 服务器异步通知页面路径
     * @param body 订单标题
     * @param remark 订单备注信息
     * @param validTime 有效时间。订单有效时间，以分为单位，默认为5
     * @param limitPay 支付限制
     * @param goodsTag 订单支付标识
     * @param benefitDetail 优惠信息
     * @param asInfo 分账信息
     * @param subbranch 门店号
     * @param extendParams 拓展参数
     * @param trueName 付款人真实姓名
     * @param idNo 证件号
     * @param unPid 银联pid
     * @param terminfo 终端信息
     * @param testFlag 是否调用测试环境
     * @return
     * @throws Exception
     */
    public JSONObject unionOrder(String api, String version, long trxAmt, String payType, String fqNum, String reQsn,
                                 String charset, String retUrl, String notifyUrl, String body, String remark,
                                 String validTime, String limitPay, String goodsTag, String benefitDetail, String asInfo,
                                 String subbranch, String extendParams, String trueName, String idNo,
                                 String unPid, String terminfo, Boolean testFlag) throws Exception {
        String url ;
        if (testFlag){
            url = config.getServerUrlMap().get("test") + Optional.ofNullable(api).orElse("apiweb/h5unionpay/unionorder");
        } else {
            url = config.getServerUrlMap().get("syb") + Optional.ofNullable(api).orElse("apiweb/h5unionpay/unionorder");
        }
        HttpConnectionUtil http = new HttpConnectionUtil(url);
        http.init();
        TreeMap<String, String> params = new TreeMap<>();
        if (StringUtils.hasLength(config.getOrgId()))
            params.put("orgid", config.getOrgId());
        params.put("cusid", config.getMchId());
        params.put("appid", config.getAppId());
        // 版本号 默认为12
        params.put("version", StringUtils.hasLength(version) ? version : "12");
        // 付款金额 单位为分
        params.put("trxamt", String.valueOf(trxAmt));
        // 指定交易类型 当指定为F02时，交易仅限分期交易。分期交易金额必须大于500元。
        params.put("paytype", payType);
        // 支付宝分期 暂只支持支付宝花呗分期,支付宝信用卡分期 仅支持A01/A02
        params.put("fqnum", fqNum);
        // 商户唯一订单号
        params.put("reqsn", reQsn);
        params.put("charset", charset);
        params.put("returl", retUrl);
        params.put("notifyUrl", notifyUrl);
        params.put("body", body);
        params.put("remark", remark);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("validtime", validTime);
        params.put("limit_pay", limitPay);
        params.put("goods_tag", goodsTag);
        params.put("benefitdetail", benefitDetail);
        params.put("asinfo", asInfo);
        params.put("subbranch", subbranch);
        params.put("extendparams", extendParams);
        params.put("signType", config.getSignType());
        params.put("trueName", trueName);
        params.put("idNo", idNo);
        params.put("unPid", unPid);
        params.put("terminfo", terminfo);

        params.put("sign", SybUtil.unionSign(params, config.getPrivateKey(), config.getSignType()));
        byte[] bys = http.postParams(params, true);
        return JSONObject.parseObject(new String(bys,"UTF-8"));
    }

    /**
     * 通联收银台： 交易退款
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=38#mid=315
     * @param version 版本号，默认填11
     * @param trxAmt 退款金额 单位为分
     * @param reQsn 商户退款订单号
     * @param oldReQsn 原交易订单号
     * @param oldTrxId 原交易流水
     * @param remark 备注
     * @param benefitDetail 优惠信息
     * @return
     */
    public JSONObject h5Refund(String version, Long trxAmt, String reQsn, String oldReQsn,
                               String oldTrxId, String remark, String benefitDetail) throws Exception {
        return h5Refund(version, trxAmt, reQsn, oldReQsn, oldTrxId, remark, benefitDetail, false);
    }

    /**
     * 通联收银台： 交易退款
     * https://aipboss.allinpay.com/know/devhelp/main.php?pid=38#mid=315
     * @param version 版本号，默认填11
     * @param trxAmt 退款金额 单位为分
     * @param reQsn 商户退款订单号
     * @param oldReQsn 原交易订单号
     * @param oldTrxId 原交易流水
     * @param remark 备注
     * @param benefitDetail 优惠信息
     * @param testFlag 是否调用测试环境
     * @return
     */
    public JSONObject h5Refund(String version, Long trxAmt, String reQsn, String oldReQsn,
                               String oldTrxId, String remark, String benefitDetail, Boolean testFlag) throws Exception {

        String url ;
        if (testFlag){
            url = config.getServerUrlMap().get("test") + "apiweb/tranx/refund";
        } else {
            url = config.getServerUrlMap().get("syb") + "apiweb/tranx/refund";
        }
        HttpConnectionUtil http = new HttpConnectionUtil(url);
        http.init();
        TreeMap<String, String> params = new TreeMap<>();
        if (StringUtils.hasLength(config.getOrgId()))
            params.put("orgid", config.getOrgId());
        params.put("cusid", config.getMchId());
        params.put("appid", config.getAppId());
        params.put("version", StringUtils.hasLength(version) ? version : "11");
        params.put("trxamt", String.valueOf(trxAmt));
        params.put("reqsn", reQsn);
        params.put("oldreqsn", oldReQsn);
        params.put("oldtrxid", oldTrxId);
        params.put("remark", remark);
        params.put("benefitdetail", benefitDetail);
        params.put("randomstr", SybUtil.getValidatecode(8));
        params.put("signtype", config.getSignType());
        params.put("sign", SybUtil.unionSign(params, config.getPrivateKey(), config.getSignType()));
        byte[] bys = http.postParams(params, true);
        return JSONObject.parseObject(new String(bys,"UTF-8"));
    }


}
